


// Node 链接  Mongodb 数据库
// mongoose 
// Mongoose是在node.js环境下对mongodb进行便捷操作的对象模型工具

const mongoose = require("mongoose")

const hostname = "127.0.0.1"
const port = 27017
const dbname = "test"
// 加密  

// mongoose.connect('mongodb://用户名:密码@127.0.0.1:27017/数据库名称')

const CONN_DB_URL = `mongodb://${hostname}:${port}/${dbname}`;
// console.log(CONN_DB_URL)

mongoose.connect(CONN_DB_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true
}, err => {
    if (err) {
        console.log("Mongodb 数据库链接异常")
    } else {
        console.log("Mongodb 数据库链接Successful")
    }
})

mongoose.connection.on('connected', function () {
    console.log('Mongoose connection open to ' + CONN_DB_URL);
});

/**
 * 连接异常
 */
mongoose.connection.on('error', function (err) {
    console.log('Mongoose connection error: ' + err);
});


/**
 * 连接断开
 */
mongoose.connection.on('disconnected', function () {
    console.log('Mongoose connection disconnected');
});